package com.jhws.business.visitor.api;

import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.business.card.bo.TblCardDimBo;
import com.jhws.business.card.bo.TblCardRecord;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.device.bo.DeviceConstant;
import com.jhws.business.visitor.bo.TblVisitorDimBo;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.common.base.bo.Global;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.business.card.service.ITblCardDimService;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.visitor.service.ITblVisitorDimService;
import com.jhws.business.card.TblCardRecordServiceImpl;
import com.jhws.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by yangtao on 2017/7/18.
 */
@SuppressWarnings("Duplicates")
@Controller
@RequestMapping("/v3/visitor")
public class VisitorApiV3 {
    @Resource
    private ITblCommDimService ICommService;
    @Resource
    private ITblBuildingDimService IBuildingDimService;
    @Resource
    private ITblVisitorDimService visitorDimService;
    @Resource
    private ITblCardDimService ICardService;
    @Resource
    private TblCardRecordServiceImpl cardRecordService;

    @Resource
    private OSSClientService ossClientService;

    /**
     * 保存访客图片
     *
     * @return
     */
    @RequestMapping(value = "anon/saveVisitorImage")
    @ResponseBody
    public JsonRespData saveVisitorImage(String deviceNum,
                                         String calledNum,
                                         String date,
                                         String type,
                                         String tempFile,
                                         String cardNum) throws Exception {
        if (StringUtils.isEmpty(type)) {
            return new JsonRespData("", -1, "类型不能为空！");
        }
        if (date == null) {
            return new JsonRespData("", -1, "日期不能为空！");
        }
        if (StringUtils.isEmpty(deviceNum)) {
            return new JsonRespData("", -1, "设备帐号不能为空！");
        }
        TblVisitorDimBo visitorDimBo = new TblVisitorDimBo();
        String commCode = AccountsUtils.getCommCode(deviceNum);
        int calledType = AccountsUtils.devicesType(calledNum);
        TblCommDimBo commDimBo;
        if (Global.VISITOR_CALL.equals(type)) {
            if (StringUtils.isEmpty(calledNum)) {
                return new JsonRespData("", -1, "被叫帐号不能为空！");
            }
            if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.OUTDOOR_TYPE) {
                commDimBo = ICommService.findTblCommDimByCommCode(commCode);
                String areaName = AccountsUtils.getBuilding(deviceNum);
                String buildingName = AccountsUtils.getUnit(deviceNum);
                String buildingAliasName = IBuildingDimService.getBuildingAliasName(commCode, areaName, buildingName);
                String unitAliasName = IBuildingDimService.getUnitAliasName(commCode,areaName,buildingName);
                String deviceName;
                deviceName = "[" + commDimBo.getCommName() + "] " + buildingAliasName + " " + unitAliasName + " 门口机-" + deviceNum.substring(11, 12);
                visitorDimBo.setBuildingAlias(buildingAliasName);
                visitorDimBo.setDeviceType(DeviceConstant.OUTDOOR_TYPE);
                visitorDimBo.setDeviceNum(deviceNum);
                visitorDimBo.setAccessDevice(deviceName);
                visitorDimBo.setType(Global.VISITOR_CALL);
                visitorDimBo.setVisitorDate(DateUtil.stringDate(date));
                visitorDimBo.setCalledNum(calledNum);
                visitorDimBo.setCommName(commDimBo.getCommName());
                visitorDimBo.setCommCode(commCode);
                visitorDimBo.setBuildingAlias(buildingAliasName);
                visitorDimBo.setCalledType(calledType);
                if (calledType == DeviceConstant.INDOOR_TYPE || calledType == DeviceConstant.MANAGE_TYPE) {
                    visitorDimBo.setBuildingCode(AccountsUtils.getBuilding(calledNum));
                    visitorDimBo.setUnitCode(AccountsUtils.getUnit(calledNum));
                    visitorDimBo.setFloorCode(AccountsUtils.getFloor(calledNum));
                    visitorDimBo.setRoomCode(AccountsUtils.getRoom(calledNum));
                } else {
                    visitorDimBo.setBuildingCode(AccountsUtils.getBuilding(deviceNum));
                    visitorDimBo.setUnitCode(AccountsUtils.getUnit(deviceNum));
                    visitorDimBo.setFloorCode(AccountsUtils.getFloor(deviceNum));
                    visitorDimBo.setRoomCode(AccountsUtils.getRoom(deviceNum));
                }
                String destKey = "outdoor/" + commCode + "/" + tempFile;
                ossClientService.visitorsCopyFile("temp_1day/" + tempFile, destKey);
                visitorDimBo.setImageUrl(destKey);
            } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.VILLA_TYPE) {
                visitorDimBo.setDeviceType(DeviceConstant.VILLA_TYPE);
                visitorDimBo.setDeviceNum(deviceNum);
                visitorDimBo.setAccessDevice("别墅机-" + deviceNum);
                visitorDimBo.setType(Global.VISITOR_CALL);
                visitorDimBo.setVisitorDate(DateUtil.stringDate(date));
                visitorDimBo.setCalledNum(calledNum);
                visitorDimBo.setCommName("别墅");
                visitorDimBo.setCalledType(calledType);
                String destKey = "villa/" + tempFile;
                ossClientService.visitorsCopyFile("temp_1day/" + tempFile, destKey);
                visitorDimBo.setImageUrl(destKey);
            } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.WALL_TYPE) {
                commDimBo = ICommService.findTblCommDimByCommCode(commCode);
                visitorDimBo.setDeviceType(DeviceConstant.WALL_TYPE);
                visitorDimBo.setDeviceNum(deviceNum);
                visitorDimBo.setAccessDevice(commUtil.getGateDevAliasName(deviceNum, commDimBo.getGateDevAliasName()));
                visitorDimBo.setType(Global.VISITOR_CALL);
                visitorDimBo.setVisitorDate(DateUtil.stringDate(date));
                visitorDimBo.setCalledNum(calledNum);
                visitorDimBo.setCommName(commDimBo.getCommName());
                visitorDimBo.setCommCode(commCode);
                visitorDimBo.setCalledType(calledType);
                if (calledType == DeviceConstant.INDOOR_TYPE || calledType == DeviceConstant.MANAGE_TYPE) {
                    String building = AccountsUtils.getBuilding(calledNum);
                    String unit = AccountsUtils.getUnit(calledNum);
                    String buildingAliasName = IBuildingDimService.getBuildingAliasName(commCode, building, unit);
                    visitorDimBo.setBuildingAlias(buildingAliasName);
                    visitorDimBo.setBuildingCode(building);
                    visitorDimBo.setUnitCode(unit);
                    visitorDimBo.setFloorCode(AccountsUtils.getFloor(calledNum));
                    visitorDimBo.setRoomCode(AccountsUtils.getRoom(calledNum));
                } else {
                    visitorDimBo.setBuildingCode(AccountsUtils.getBuilding(deviceNum));
                    visitorDimBo.setUnitCode(AccountsUtils.getUnit(deviceNum));
                    visitorDimBo.setFloorCode(AccountsUtils.getFloor(deviceNum));
                    visitorDimBo.setRoomCode(AccountsUtils.getRoom(deviceNum));
                }
                String destKey = "outdoor/" + commCode + "/" + tempFile;
                ossClientService.visitorsCopyFile("temp_1day/" + tempFile, destKey);
                visitorDimBo.setImageUrl(destKey);
            } else {
                return new JsonRespData("", -1, "设备帐号错误！");
            }
            visitorDimService.save(visitorDimBo);
        } else if (Global.VISITOR_CARD.equals(type)) {
            if (StringUtils.isEmpty(cardNum)) {
                return new JsonRespData("", -1, "卡号不能为空！");
            }
            TblCardRecord cardRecord = new TblCardRecord();
            TblCardDimBo cardDimBo = ICardService.findCardInfo(cardNum);
            if (cardDimBo != null) {
                cardRecord.setCardNo(cardDimBo.getCardNo());
                cardRecord.setCardType(cardDimBo.getCardType());
                cardRecord.setCommCode(cardDimBo.getCommCode());
                cardRecord.setOwnerCode(cardDimBo.getOwnerCode());
                cardRecord.setOwnerName(cardDimBo.getOwnerName());
                cardRecord.setRoomName(cardDimBo.getRoomName());
                /*SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String dateString = formatter.format(date);*/
                cardRecord.setSwipeCardTime(DateUtil.stringDate(date));
                if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.OUTDOOR_TYPE) {
                    commDimBo = ICommService.findTblCommDimByCommCode(commCode);
                    cardRecord.setCommName(commDimBo.getCommName());
                    String areaName = AccountsUtils.getBuilding(deviceNum);
                    String buildingName = AccountsUtils.getUnit(deviceNum);
                    String buildingAliasName = IBuildingDimService.getBuildingAliasName(commCode, areaName, buildingName);
                    String unitAliasName = IBuildingDimService.getUnitAliasName(commCode,areaName,buildingName);
                    String deviceName = "[" + commDimBo.getCommName() + "] " + buildingAliasName + " " + unitAliasName + " 门口机-" + deviceNum.substring(11, 12);
                    cardRecord.setDeviceInfo(deviceName);
                } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.VILLA_TYPE) {
                    cardRecord.setDeviceInfo("别墅机-" + deviceNum);
                } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.WALL_TYPE) {
                    commDimBo = ICommService.findTblCommDimByCommCode(commCode);
                    cardRecord.setCommName(commDimBo.getCommName());
                    cardRecord.setDeviceInfo(commUtil.getGateDevAliasName(deviceNum, commDimBo.getGateDevAliasName()));
                } else {
                    return new JsonRespData("", -1, "设备帐号错误！");
                }
                String destKey = "card/" + tempFile;
                ossClientService.visitorsCopyFile("temp_1day/" + tempFile, destKey);
                cardRecord.setImageUrl(destKey);
                cardRecordService.save(cardRecord);
            } else {
                return new JsonRespData("", -1, "该卡没有登记！");
            }
        } else {
            return new JsonRespData("", -1, "类型不存在！");
        }
        return new JsonRespData("", 1, "保存成功");
    }

    /**
     * 呼叫照片查询
     *
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "anon/queryVisitor")
    @ResponseBody
    public JsonRespData queryVisitor(@RequestParam Map param) throws Exception {
        List<Map<String, Object>> visitorList = visitorDimService.findVisitorListApi(param);
        return new JsonRespData("", 1, visitorList);
    }
}
